<?php
namespace App\Http\Controllers\Admin;

use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\ResponseUtil;
use App\DTOS\Admin\LogRoleChatSearchDTO;
use App\Services\LogRoleChatService;
use Illuminate\Http\Request;

// 聊天日志
class LogRoleChatController
{
    private LogRoleChatService $service;
    private UserRoleService $userRoleService;
    private UserMemberService $userMemberService;
    private Request $request;

    /**
     * LogRoleChatController constructor.
     * @param LogRoleChatService $service
     * @param Request $request
     * @param UserRoleService $userRoleService
     * @param UserMemberService $userMemberService
     */
    public function __construct(LogRoleChatService $service,Request $request, UserRoleService $userRoleService, UserMemberService $userMemberService)
    {
        $this->service = $service;
        $this->userRoleService = $userRoleService;
        $this->userMemberService = $userMemberService;
        $this->request = $request;
    }


    public function paginate()
    {
        $DTO = new LogRoleChatSearchDTO(request()->all());
        $severid= $DTO->getGameArea();
        $paginate = $this->service->paginate($DTO);
        $data = $paginate->items();
        if ($severid != 901){

            $roleIds = collect($data)->pluck('roleId')->unique()->toArray();
            $memIds = collect($data)->pluck('memId')->unique()->toArray();
            $members = $this->userMemberService->getByIds($memIds);
            $userRoles = $this->userRoleService->getByIds($roleIds,['id','nick','memid']);
            foreach ($data as &$datum){
                $member = collect($members)->where('id',$datum['memId'])->first();
                $role = collect($userRoles)->where('id',$datum['roleId'])->first();
                $datum['member'] = $member;
                $datum['role'] = $role;
            }
        }
        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        $res['count'] = $paginate->total();
        return ResponseUtil::json($res);
    }

}
